<template>
  <div class="add">
    <van-nav-bar title="添加地址" left-arrow @click-left="onClickLeft" />
    <van-address-edit
      ref="Cdd"
      :area-list="areaList"
      show-set-default
      show-search-result
      tel-maxlength="11"
      :search-result="searchResult"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @change-detail="onChangeDetail"
    />
  </div>
</template>

<script>
import axios from "axios";
import { Toast } from "vant";
export default {
  methods: {
    onClickLeft() {
      this.$router.push("/address");
    },
    onSave(content) {
      Toast("保存成功");
      console.log("content", content);
      let userName = content.name;
      let sex = 1;
      let tel = content.tel;
      let detailAddr = content.province + content.city + content.county;
      let houseNumber = content.addressDetail;
      let label = "家";
      let defaultAddr = 0;
      // console.log(
      //   userName,
      //   sex,
      //   tel,
      //   detailAddr,
      //   houseNumber,
      //   label,
      //   defaultAddr
      // );

      axios({
        url: "/apf/addr/addAddr",
        method: "POST",
        data: {
          defaultAddr: defaultAddr,
          detailAddr: detailAddr,
          houseNumber: houseNumber,
          id: 0,
          label: label,
          sex: sex,
          tel: tel,
          userId: 2,
          userName: userName,
        },
      }).then((res) => {
        console.log(res.data.data);
      });
      this.$router.push("/address");
    },
    onChangeDetail(val) {
      if (val) {
        this.searchResult = [
          {
            name: "黄龙万科中心",
            address: "杭州市西湖区",
          },
        ];
      } else {
        this.searchResult = [];
      }
    },
  },
  data() {
    return {
      areaList: {
        province_list: {
          110000: "北京市",
          120000: "天津市",
        },
        city_list: {
          110100: "北京市",
          120100: "天津市",
        },
        county_list: {
          110101: "东城区",
          110102: "西城区",
          110103: "朝阳区",
        },
      },
      searchResult: [],
    };
  },
};
</script>

<style scoped>
/deep/.van-nav-bar {
  background-color: #f5f5f5;
}
/deep/.van-nav-bar .van-icon {
  color: gray;
}

/deep/.van-switch--on {
  background-color: #6d87c4;
}
/deep/.van-button--round {
  background-color: #6d87c4;
  border: none;
}
.add {
  height: 6.4rem;
  background-color: #f5f5f5;
}
</style>